package homework;
import java.util.Stack;

//力扣844题、比较含退格的字符串(简单)
public class backspaceCompare {
    public static boolean backspaceCompare(String s, String t) {
        Stack<Character> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if (ch != '#'){
                stack1.push(ch);
            }else{
                if (!stack1.empty()){
                    stack1.pop();
                }
            }
        }
        for (int i = 0; i < t.length(); i++){
            char ch = t.charAt(i);
            if (ch != '#'){
                stack2.push(ch);
            }else{
                if (!stack2.empty()){
                    stack2.pop();
                }
            }
        }
        while (!stack1.empty() && !stack2.empty()){
            if (stack1.pop() != stack2.pop()){
                return false;
            }
        }
        if (stack1.empty() && stack2.empty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String s = "ab##";
        String t = "c#b#";
//        String s = "ab#c";
//        String t = "ad#c#";
        boolean flag = backspaceCompare(s, t);
        System.out.println(flag);
    }
}